package com.hwd.gmall.seckill.config;

import com.hwd.gmall.common.constant.Const;
import com.hwd.gmall.common.constant.SeckillConst;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 秒杀商品数据同步的消息队列的配置
 *
 * @author 黄伟东/Victor
 * @date 2022/05/12 20:37
 */
@Configuration
public class SeckillGoodsRabbitConfig {

    /**
     * 正常交换机
     *
     * @return org.springframework.amqp.core.Exchange
     */
    @Bean("seckillGoodsNormalExchange")
    public Exchange seckillGoodsNormalExchange() {
        return ExchangeBuilder.directExchange(SeckillConst.SECKILL_GOODS_NORMAL_EXCHANGE).build();
    }

    /**
     * 死信队列
     *
     * @return org.springframework.amqp.core.Queue
     */
    @Bean("seckillGoodsDeadQueue")
    public Queue seckillGoodsDeadQueue() {
        return QueueBuilder.durable(SeckillConst.SECKILL_GOODS_DEAD_QUEUE)
                .withArgument(Const.X_DEAD_LETTER_EXCHANGE,SeckillConst.SECKILL_GOODS_DEAD_EXCHANGE)
                .withArgument(Const.X_DEAD_LETTER_ROUTING_KEY,SeckillConst.SECKILL_GOODS_NORMAL_ROUTING_KEY)
                .build();
    }

    /**
     * 正常交换机和死信队列进行绑定
     *
     * @param seckillGoodsNormalExchange 正常交换机
     * @param seckillGoodsDeadQueue      队列
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding seckillGoodsDeadBinding(@Qualifier("seckillGoodsNormalExchange") Exchange seckillGoodsNormalExchange,
                                           @Qualifier("seckillGoodsDeadQueue") Queue seckillGoodsDeadQueue) {
        return BindingBuilder.bind(seckillGoodsDeadQueue)
                .to(seckillGoodsNormalExchange)
                .with(SeckillConst.SECKILL_GOODS_DEAD_ROUTING_KEY)
                .noargs();
    }

    /**
     * 死信交换机
     *
     * @return org.springframework.amqp.core.Exchange
     */
    @Bean("seckillGoodsDeadExchange")
    public Exchange seckillGoodsDeadExchange() {
        return ExchangeBuilder.directExchange(SeckillConst.SECKILL_GOODS_DEAD_EXCHANGE).build();
    }

    /**
     * 正常队列
     *
     * @return org.springframework.amqp.core.Queue
     */
    @Bean("seckillGoodsNormalQueue")
    public Queue seckillGoodsNormalQueue() {
        return QueueBuilder.durable(SeckillConst.SECKILL_GOODS_NORMAL_QUEUE).build();
    }

    /**
     * 死信交换机和正常队列进行绑定
     *
     * @param seckillGoodsDeadExchange 属性交换机
     * @param seckillGoodsNormalQueue  正常队列
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding seckillGoodsNormalBinding(@Qualifier("seckillGoodsDeadExchange") Exchange seckillGoodsDeadExchange,
                                             @Qualifier("seckillGoodsNormalQueue") Queue seckillGoodsNormalQueue) {
        return BindingBuilder.bind(seckillGoodsNormalQueue)
                .to(seckillGoodsDeadExchange)
                .with(SeckillConst.SECKILL_GOODS_NORMAL_ROUTING_KEY)
                .noargs();
    }
}
